
//constants
default_persistence = 1209600000; //14days
param_urls_prefix = "_u";
param_persistence = "_p";

var generate_test_url = function(){
	//params
	var params = $(".test_parameter").serialize();
	//remove default persistence
	params = params.replace("&"+param_persistence+"="+default_persistence,"");
	//url
	testurl = location.protocol + "//" + location.hostname + (location.port?":"+location.port:"") + "/t/?" + params;
    $("#testurl").val(testurl);
	//update go button
	$("#go").attr("href", $("#testurl").val()); 
}

var parse_test_url = function(){
	//iterate form fields to fetch query paremeter
	$(".test_parameter").each(function(){
		var re = new RegExp(""+$(this).attr("name")+"=([^&#]+)");
		var matches = re.exec($("#testurl").val());
		if (matches != null){
			$(this).val(decodeURIComponent(matches[1]));
		}
	});
	//update go button
	$("#go").attr("href", $("#testurl").val()); 
}

//build url
$(".test_parameter").change(generate_test_url);
$(".test_parameter").keypress(generate_test_url);
$("#testurl").change(parse_test_url);
$("#testurl").keypress(parse_test_url);


//form interactions
var load_default_value = function(){
	if ($.trim($(this).val())==''){
        $(this).val($(this).attr("title"));
      }
}
$(".test_parameter, #testurl").focusin(function(){
	if ($(this).val()==$(this).attr("title")){
        $(this).val('');
      }
})
$(".test_parameter, #testurl").focusout(load_default_value);
//auto-select
$("#testurl").click(function(){
	 $(this).select();
});

//onload
$(document).ready(function(){
    //load default values
    $(".test_parameter, #testurl").each(load_default_value);
 	$("#persistence").each(function(){
      if ($.trim($(this).val())==''){
        $(this).val(default_persistence);
      }
    });
	//go
	$("#go").button();
});

//update go button
$("#go").mousedown(function(){
	$("#go").attr("href", $("#testurl").val());
});



